'''
This File Read CSV and Sorted
CSV File Like: 0:类型, 1:测试点, 2:等价类, 3:值, 4:结果
Like:[(Type,testPoint,isValuable,content,result),...]
Like
Saved in OrderedDict
'''

import random
# Tool for Parser
from Tools.tools import CSVReader, JSONReader, LOG
# Import A OrderedDict Which Can Sorted testCase convenient
from collections import OrderedDict


class Parser:

    # first Disorder List
    testContentList = list()
    # first Disorder Dictionary
    testContentDict = dict()
    # second with Sequence Dictionary
    testContentDict_Num = dict()
    # third Ordered Dictionary
    testContentOrderedDict = OrderedDict()

    def __init__(self):
        LOG().DEBUG('开始预读取数据...')

    def parser1(self, menu='港股通委托'):
        '''
        HongKong Stock Trade Parser
        :return: None, Data Saved into testContentDict
        '''
        for schema in CSVReader():
            market = random.choice(JSONReader('market')[:2])
            deal = random.choice(JSONReader('deal'))
            for type_ in JSONReader('type_'):
                if type_ in schema[0] or schema[0] == "全部":
                    testContent = '_'.join([menu, market, deal, type_, schema[1]]) + '：' + schema[3]
                    self.testContentList.append(testContent)
                    self.testContentDict[testContent] = {'isAvailable':schema[2],
                                                         'testResult':schema[4]}
                    LOG().DEBUG('港股通案例初始化成功')

    def parser2(self, menu = '新三板委托'):
        '''
        National Equality Exchange and Quotation Parser
        :return: None, Data Saved into testContentDict
        '''
        for schema in CSVReader():
            market = random.choice(JSONReader('market')[2:])
            deal = random.choice(JSONReader('deal')[:2])
            for type_ in JSONReader('type_'):
                if type_ in schema[0] or schema[0] == "全部":
                    if random.randint(0, 2) != 0:
                        testContent = '_'.join([menu, market, deal, type_, schema[1]]) + '：' + schema[3]
                        self.testContentList.append(testContent)
                        self.testContentDict[testContent] = {'isAvailable': schema[2],
                                                             'testResult': schema[4]}
                        LOG().DEBUG('新三板案例初始化成功')
                    else:
                        pass

    def sortedMethod(self, item:str, x:int):
        '''
        Function for Sorted
        :param item: for key in testContentDict.keys()
        :return: testContentDict_Num[i] = key
        '''
        a = 20000 if "港股通委托" in item else 0
        b = 10000 if "新三板委托" in item else 0
        c = random.randint(1000, 1001) if "_深港通_" in item else 0
        d = random.randint(1000, 1001) if "_沪港通_" in item else 0
        e = 1000 if "_新三板_" in item else 0
        f = random.randint(100, 101) if "_买入_" in item else 0
        j = random.randint(100, 101) if "_卖出_" in item else 0
        h = 80 if "_卖碎股_" in item else 0
        i = 60 if "_策略埋单_" in item else 0
        g = 40 if "_策略拆单_" in item else 0
        k = 20 if "_网格交易_" in item else 0
        l = 10 if "_强势弱势股_" in item else 0
        m = 5 if "_跟踪止损_" in item else 0
        n = self.testContentList.index(item) * 0.0003
        self.testContentDict_Num[sum([a, b, c, d, e, f, g, h, i, j, k, l, m, n])- x * 0.0007 ] = item
        # return item + '|||' + str(sum([a, b, c, d, e, f, g, h, i, g, k, l, m, n]))

    def sortedDict(self):
        self.parser1()
        self.parser2()
        sequence = []
        x = 0
        for key in self.testContentDict.keys():
            x += 1
            self.sortedMethod(key, x)
        sequence = self.testContentDict_Num.keys()
        sequence = sorted(sequence,reverse=True)
        for s in sequence:
            self.testContentOrderedDict[self.testContentDict_Num[s]] = self.testContentDict[self.testContentDict_Num[s]]
        LOG().DEBUG('案例准备就绪')


if __name__ == '__main__':
    Parser().sortedDict()
    for k,v in Parser().testContentOrderedDict.items():
        print(k,v)




